package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.persistence.dao.BillCheckMapper;
import com.stylefeng.guns.common.persistence.dao.TBillMapper;
import com.stylefeng.guns.common.persistence.model.BillCheck;
import com.stylefeng.guns.common.persistence.model.TBill;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.service.ITBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 流水账控制器
 *
 * @author fengshuonan
 * @Date 2018-03-07 21:12:35
 */
@Controller
@RequestMapping("/tBill")
public class TBillController extends BaseController {

    private String PREFIX = "/system/tBill/";

    @Autowired
    private ITBillService tBillService;

    @Autowired
    private TBillMapper tBillMapper;

    @Autowired
    private BillCheckMapper billCheckMapper;

    /**
     * 跳转到流水账首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tBill.html";
    }

    /**
     * 跳转到添加流水账
     */
    @RequestMapping("/tBill_add")
    public String tBillAdd() {
        return PREFIX + "tBill_add.html";
    }

    /**
     * 跳转到修改流水账
     */
    @RequestMapping("/tBill_update/{tBillId}")
    public String tBillUpdate(@PathVariable Integer tBillId, Model model) {
        TBill tBill = tBillService.selectById(tBillId);
        model.addAttribute("item",tBill);
        LogObjectHolder.me().set(tBill);
        return PREFIX + "tBill_edit.html";
    }
    /**
     * 账目核算
     */
    @RequestMapping("/accounting")
    public String accounting(Model model) {
        //最近一条结算记录
        BillCheck billCheck =billCheckMapper.selectListMaxCheckBill(ShiroKit.getUser().getDotId());
        String beginTime="";
        if(billCheck!=null&&billCheck.getEndTime()!=null){
            Calendar c = Calendar.getInstance();
            c.setTime(billCheck.getEndTime());
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date tomorrow = c.getTime();
            beginTime=DateUtil.getDay(tomorrow)+" 00:00:00";
        }else{
            TBill tBill = tBillMapper.selectListMinUnCheckBill(ShiroKit.getUser().getDotId());
            beginTime= DateUtil.getDay(tBill.getCreateTime())+" 00:00:00";
        }
        model.addAttribute("beginTime",beginTime);
        model.addAttribute("endTime",beginTime.split(" +")[0]+" 23:59:59"); //同一天
        return PREFIX + "accounting.html";
    }
    /**
     * 历史对账
     */
    @RequestMapping("/accountingRecord")
    public String accountingRecord() {
        return PREFIX + "accountingRecord.html";
    }
    /**
     * 历史对账明细
     */
    @RequestMapping("/accountingRecordDetail/{id}")
    public String accountingRecordDetail(@PathVariable("id") Integer id,Model model) {
        model.addAttribute("id",id);
        return PREFIX + "accountingRecordDetail.html";
    }

    /**
     * 获取需要核对的账单
     */
    @RequestMapping(value = "/accountingList")
    @ResponseBody
    public Object accountingList(Integer offset,Integer limit ) {
        Map m=new HashMap<>();
        Page<Map<String, Object>> page = new Page(0, 100000);//查询一天记录
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String condition=getPara("condition");
        String beginTime=getPara("beginTime");
        String endTime=getPara("endTime");
        String code=getPara("code");
        String income=getPara("income");
        Map param=new HashMap();
        param.put("dot_id", ShiroKit.getUser().getDotId() + "");
        param.put("condition",condition);
        param.put("beginTime",beginTime);
        param.put("code",code);
        param.put("endTime",endTime==null?beginTime.split(" +")[0]+" 23:59:59":endTime);
        param.put("income",income);
        List<Map<String, Object>> list = tBillService.selectAccountingList(page, param);
        pageInfoBT.setData(list);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }


    /**
     * 获取需要核对的账单
     */
    @RequestMapping(value = "/accountingRecordDetailList")
    @ResponseBody
    public Object accountingRecordDetailList(Integer offset,Integer limit ) {
        Map m=new HashMap<>();
        Page<Map<String, Object>> page = new Page(0, 100000);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String bill_check_id=getPara("bill_check_id");
        Map param=new HashMap();
        param.put("dot_id", ShiroKit.getUser().getDotId() + "");
        param.put("bill_check_id",bill_check_id);
        List<Map<String, Object>> list = tBillService.selectAccountingRecordDetailList(page, param);
        pageInfoBT.setData(list);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 历史对账记录
     */
    @RequestMapping(value = "/accountingRecordList")
    @ResponseBody
    public Object accountingRecordList(Integer offset,Integer limit ) {
        Map m=new HashMap<>();
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String beginTime=getPara("beginTime");
        String endTime=getPara("endTime");
        Map param=new HashMap();
        param.put("dot_id", ShiroKit.getUser().getDotId() + "");
        param.put("beginTime",beginTime);
        param.put("endTime",endTime);
        List<Map<String, Object>> list = tBillService.selectAccountingRecordList(page, param);
        pageInfoBT.setData(list);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 获取流水账列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return tBillService.selectList(null);
    }

    /**
     * 新增流水账
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TBill tBill) {
        tBillService.insert(tBill);
        return super.SUCCESS_TIP;
    }
    /**
     * 新增流水账
     */
    @RequestMapping(value = "/confirmTBill")
    @ResponseBody
    public Object confirmTBill(String ids ,BillCheck bill) {
        bill.setBillCheckNum("HD"+ ShiroKit.getUser().getId()+"_"+ DateUtil.getAllTimes());
        bill.setEditUser(ShiroKit.getUser().getId());
        bill.setDotId(ShiroKit.getUser().getDotId());
        String beginTime1=getPara("beginTime1");
        String endTime1=getPara("endTime1");
        bill.setBeginTime(DateUtil.parseTime(beginTime1));
        bill.setEndTime(DateUtil.parseTime(endTime1));
         tBillService.confirmTBill(ids, bill);
        return super.SUCCESS_TIP;
    }


    /**
     * 删除流水账
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tBillId) {
        tBillService.deleteById(tBillId);
        return SUCCESS_TIP;
    }

    /**
     * 修改流水账
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TBill tBill) {
        tBillService.updateById(tBill);
        return super.SUCCESS_TIP;
    }

    /**
     * 流水账详情
     */
    @RequestMapping(value = "/detail/{tBillId}")
    @ResponseBody
    public Object detail(@PathVariable("tBillId") Integer tBillId) {
        return tBillService.selectById(tBillId);
    }
}
